<script setup lang="ts">
import { onMounted, ref } from "vue";
import ReCol from "@/components/ReCol";
import { formRules } from "./utils/rule";
import { FormProps, usePublicHooks } from "@/views/hooks";
import { Dept, getDeptList } from "@/api/dept";
import { handleTree } from "@/utils/tree";

const props = withDefaults(defineProps<FormProps<Dept>>(), {
  formInline: () => ({})
});

const ruleFormRef = ref();
const { switchStyle } = usePublicHooks();
const newFormInline = ref(props.formInline);
const options = ref<Array<any>>([]);

function getRef() {
  return ruleFormRef.value;
}
function loadDeptTop() {
  getDeptList().then(data => {
    options.value = handleTree(data); // 处理成树结构
  });
}

onMounted(() => {
  loadDeptTop();
});

defineExpose({ getRef });
</script>

<template>
  <el-form
    ref="ruleFormRef"
    :model="newFormInline"
    :rules="formRules"
    label-width="82px"
  >
    <el-row :gutter="30">
      <re-col>
        <el-form-item label="上级部门" prop="parentId">
          <el-cascader
            v-model="newFormInline.parentId"
            class="w-full"
            :options="options"
            :props="{
              value: 'id',
              label: 'name',
              leaf: 'isLeaf',
              emitPath: false,
              checkStrictly: true
            }"
            clearable
            filterable
            placeholder="请选择上级部门"
          >
            <template #default="{ node, data }">
              <span>{{ data.name }}</span>
              <span v-if="!node.isLeaf"> ({{ data.children.length }}) </span>
            </template>
          </el-cascader>
        </el-form-item>
      </re-col>

      <re-col :value="12" :xs="24" :sm="24">
        <el-form-item label="部门名称" prop="name">
          <el-input
            v-model="newFormInline.name"
            clearable
            placeholder="请输入部门名称"
          />
        </el-form-item>
      </re-col>
      <re-col :value="12" :xs="24" :sm="24">
        <el-form-item label="部门负责人">
          <el-input
            v-model="newFormInline.leaderId"
            clearable
            placeholder="请输入部门负责人"
          />
        </el-form-item>
      </re-col>
      <re-col :value="12" :xs="24" :sm="24">
        <el-form-item label="排序">
          <el-input-number
            v-model="newFormInline.sort"
            :min="0"
            :max="9999"
            controls-position="right"
          />
        </el-form-item>
      </re-col>
      <re-col :value="12" :xs="24" :sm="24">
        <el-form-item label="部门状态">
          <el-switch
            v-model="newFormInline.state"
            inline-prompt
            :active-value="true"
            :inactive-value="false"
            active-text="启用"
            inactive-text="停用"
            :style="switchStyle"
          />
        </el-form-item>
      </re-col>
      <re-col>
        <el-form-item label="备注">
          <el-input
            v-model="newFormInline.description"
            placeholder="请输入备注信息"
          />
        </el-form-item>
      </re-col>
    </el-row>
  </el-form>
</template>
